<template>
  <div class="app-container">
    <el-dialog title="补料物料信息选择" :visible.sync="open" width="960px" append-to-body>
      <el-table
        v-loading="loading"
        :data="detailList"
        @selection-change="handleSelectionChange"
        :row-class-name="tableRowClassName"
        height="70vh"
      >
        <el-table-column type="selection" width="55" align="center" v-if="optType!=='check'" />
        <el-table-column type="index" label="序号" align="center" width="50" />
        <el-table-column label="物料编码" align="center" prop="itemCode" />
        <el-table-column label="物料名称" align="center" prop="sonname" />
        <el-table-column label="规格" align="center" prop="specification" />
        <el-table-column label="单位" align="center" prop="unitOfMeasure" />
        <!-- <el-table-column label="数量" align="center" prop="itemAmount">
          <template slot-scope="scope">
            <el-input v-model="scope.row.itemAmount" placeholder="请输入数量" v-if="optType!=='check'"></el-input>
            <span v-if="optType==='check'">{{scope.row.itemAmount}}</span>
          </template>
        </el-table-column>-->
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listDetail,
  getDetail,
  delDetail,
  addDetail,
  updateDetail
} from "@/api/mes/pro/supplyDetail";
import { listMaterialInfo } from "@/api/mes/pro/supply";

export default {
  name: "supplyDetail",
  props: ["optType", "parentId"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产补料详细信息表格数据
      detailList: [],
      //   选中信息
      checkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //勾选的内容
      selectData: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialSerial: null,
        productCode: null,
        productName: null,
        productSpec: null,
        measureName: null,
        itemAmount: null,
        supplyId: this.parentId,
        suppyTime: null,
        workorderCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      checkListAll: []
    };
  },
  created() {},
  methods: {
    init(row, list) {
      this.form = row;
      this.checkListAll = list;
      this.open = true;
      this.refreshMaterialList(this.form.workorderCode);
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.info("selection===", selection);
      this.checkList = selection;
    },
    refreshMaterialList(workorderCode) {
      this.loading = true;
      this.queryParams.workorderCode = this.form.workorderCode;
      this.queryParams.bomItemCode = this.form.bomItemCode; 
      listMaterialInfo(this.queryParams).then(response => {
        this.detailList = response.data;
        this.loading = false;
      });
    },
    submitForm() {
      this.ids = this.checkList.map(item => item.itemCode);
      let list = this.checkListAll.map(item => item.itemCode);
      let repetitive = this.compare(list, this.ids);
      console.log(repetitive, "之前提交了 现在又选了的");
      if (repetitive.length != 0) {
        this.$modal.msgError("产品物料编码为" + repetitive + "已被选择");
      } else {
        console.log(this.checkList, "======this.checkList=======");
        this.$emit("checkQuery", this.checkList);
        this.cancel();
      }
    },

    //改变颜色
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, "row");
      // console.log(this.list, "this.list");
      let bool = this.checkListAll.filter(
        _ => _.itemCode == row.itemCode
      );
      if (bool && bool.length > 0) {
        return "success-row";
      }
      return "";
    },
    compare(arr1, arr2) {
      return arr1.filter(v => {
        return arr2.includes(v);
      });
    }
  }
};
</script>
<style >
.el-table .warning-row {
  background: oldlace;
}
.el-table .success-row {
  background: rgba(241, 107, 107, 0.743);
}
</style>